<!--
 * @Author: tkp
 * @Date: 2024-03-29 10:17:43
-->
<template>
  <div>
    <el-form :model="queryForm" label-position="right" :rules="rules" ref="ruleForm" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item prop="systemId" label="所属系统:">
            <el-radio-group v-model="queryForm.systemId" @input="inputHandle">
              <el-radio v-for="(item) in sysTeamType" :key="item.systemId" :label="item.systemId">{{ item.systemName }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="上级菜单:" prop="parentsId">
            <treeselect v-model="queryForm.parentsId" :options="menuOptions" :normalizer="normalizer" :show-count="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item prop="moduleId" label="系统模块:">
            <Select size="medium" placeholder="请选择系统模块" :clearable="false" v-model="queryForm.moduleId" :data="moduleLst" :jsonType="{'value':'moduleId','label':'moduleName'}"></Select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="menuName" label="菜单名称:">
            <Input v-model="queryForm.menuName" size="medium" clearable placeholder="请输入菜单名称"></Input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="buttonFlag" label="类型:">
            <el-radio-group v-model="queryForm.buttonFlag">
              <el-radio :label="0">菜单</el-radio>
              <el-radio :label="1">按钮</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="enable" label="是否可用:">
            <el-radio-group v-model="queryForm.enable">
              <el-radio :label="0">不可用</el-radio>
              <el-radio :label="1">可用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="visible" label="是否可见:">
            <el-radio-group v-model="queryForm.visible">
              <el-radio :label="0">不可见</el-radio>
              <el-radio :label="1">可见</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="queryForm.buttonFlag==0">
          <el-form-item prop="frontPath" label="前端路径:">
            <Input v-model="queryForm.frontPath" size="medium" clearable placeholder="请输入前端路径"></Input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="frontRoute" label="前端路由:">
            <Input v-model="queryForm.frontRoute" size="medium" clearable placeholder="请输入前端路由"></Input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="queryForm.buttonFlag==0">
          <el-form-item prop="iconUrl" label="图标路径:">
            <Input v-model="queryForm.iconUrl" size="medium" clearable placeholder="请输入图标路径"></Input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item prop="sortIndex" label="排序:">
            <Input v-model="queryForm.sortIndex" size="medium" type="number" clearable placeholder="请输入排序"></Input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item prop="interfaceIds" label="请求接口资源:">
            <el-select style="width:100%" v-model="queryForm.interfaceIds" multiple filterable remote placeholder="请输入接口资源" :remote-method="remoteMethodEmp">
              <el-option v-for="item in interFaceLst" :key="item.interfaceId" :label="item.interfaceName" :value="item.interfaceId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" size="medium" @click="okPop">确认</el-button>
        <el-button size="medium" @click="closePop">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { authModuleQuery, authInterfaceQuery, authSystemQuery } from '@/utils/api'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: {
    Treeselect
  },
  props: {
    data: {
      type: Object | String,
      required: true
    },
    tableData: {
      type: Array,
      required: true
    },
  },
  data() {
    return {
      menuOptions: [{ menuId: 0, menuName: '顶级菜单', children: [] }],
      queryForm: {
        systemId: '',
        parentsId: '',
        moduleId: '',
        menuName: '',
        frontPath: '',
        frontRoute: '',
        iconUrl: '',
        buttonFlag: '',
        enable: '',
        visible: '',
        interfaceIds: '',
        level: 1,
        sortIndex: '',
      },
      moduleLst: [],
      interFaceLst: [],
      sysTeamType: [],
      rules: {
        systemId: [
          { required: true, message: '此项为必填' },
        ],
        parentsId: [
          { required: true, message: '此项为必填' },
        ],
        moduleId: [
          { required: true, message: '此项为必填' },
        ],
        menuName: [
          { required: true, message: '此项为必填' },
        ],
        frontPath: [
          { required: true, message: '此项为必填' },
        ],
        frontRoute: [
          { required: true, message: '此项为必填' },
        ],
        buttonFlag: [
          { required: true, message: '此项为必填' },
        ],
        iconUrl: [
          { required: true, message: '此项为必填' },
        ],
        enable: [
          { required: true, message: '此项为必填' },
        ],
        visible: [
          { required: true, message: '此项为必填' },
        ],
        sortIndex: [
          { required: true, message: '此项为必填' },
        ],
      },
    }
  },
  created() {
    this.menuOptions[0].children = this.tableData || []
  },
  mounted() {
    this.loadSystem()
    if (this.data) {
      this.queryForm.level = this.data.level + 1
      this.queryForm.parentsId = this.data.menuId
      this.queryForm.systemId = this.data.systemId
    } else {
      this.queryForm.parentsId = this.menuOptions[0].menuId
    }
    this.getModule()
  },
  methods: {
    closePop() {
      this.$emit('addData', '')
    },
    okPop() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.$emit('addData', this.queryForm)
        } else {
          this.messageWarnPop('请填写完整')
          return false;
        }
      });
    },
    remoteMethodEmp(val) {
      this.getInterfaceList(val)
    },
    getInterfaceList(val) {
      let param = {
        params: {
          interfaceName: val || '',
          // systemId: this.queryForm.systemId,
        },
        page: {
          pageNum: 1,
          pageSize: 1500,
        }
      }
      authInterfaceQuery(param).then(res => {
        this.interFaceLst = res.data.lst
      })
    },
    inputHandle(e) {
      this.queryForm.moduleId = ''
      this.getModule()
      this.getInterfaceList()
    },
    getModule() {
      let param = {
        pageNum: 1,
        pageSize: 2000,
        systemId: this.queryForm.systemId,
      }
      authModuleQuery(param).then(res => {
        this.moduleLst = res.data
      })
    },
    loadSystem() {
      let param = {
        enable: 1
      }
      authSystemQuery(param).then(res => {
        this.sysTeamType = res.data || []
      })
    },
    normalizer(node) {
      // if (node.children && !node.children.length) {
      //   delete node.children;
      // }
      if (node.children == null || node.children == 'null') {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
  }
}
</script>

<style lang="scss" scoped>
.fenrun_class {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 5px;
  .title {
    width: 100px;
  }
  .end {
    color: #999999;
  }
}
/deep/ .el-radio {
  margin-bottom: 10px;
}
</style>
